using System;
using System.Text;
using System.Collections;

namespace codebase.UI
{
    public abstract class Window
    {
        public int Left;
        public int Top;
        public int Width;
        public int Height;

#if NDS 
        public NDSTouchscreen touchscreen;
#endif
        public WindowManager Parent;

        public int CaptionColor = ColorExtensions.BLACK16;
        public int BackgroundColor = ColorExtensions.WHITE16;
        public int BorderColor = ColorExtensions.BLACK16;
        public int ForeColor = ColorExtensions.WHITE16;
        public int EffectColor = ColorExtensions.WHITE16;

        public bool DrawBorder = true;
        public bool FullScreen = false;
        public bool Visible = false;
        public bool Gradient = false;
        public GradientType GradientMode = GradientType.Blue;
        public int GradientStart = 160;
        public int GradientEnd = 50;
        public bool Effect = false;
        public EffectType EffectMode = EffectType.Squares;

        public enum EffectType
        {
            Squares,
            Circles
        }

        public enum GradientType
        {
            Red,
            Green,
            Blue
        }

        public string Text = null;

        ArrayList Controls = new ArrayList();

        public enum ControlTypes
        {
            Label = 1,
            RadioButton  = 2,
            CheckBox = 3,
            ScrollingGraph = 4,
            Image = 5,
            Button = 6,
            ListBox = 7,
            Frame = 8,
            Clock = 9
        }

        public virtual void Initialize(WindowManager win, int width, int height)
        {
            Width = width * win.screen.CharacterWidth;
            Height = height * win.screen.CharacterHeight;

#if NDS
            touchscreen = win.touchscreen;
#endif
            Parent = win;

            win.Add(this);
        }

#if !PC

        bool isMoving = false;
        int offsetX = 0;

        public void touchscreen_TouchDownEvent(object o, TouchEventArgs e)
        {
            if (!Visible) return;

            int x = e.x;
            int y = e.y;

            if (!FullScreen && Text != null)
            {
                if (x >= Left && x <= Left + Width && y >= Top && y <= Top)
                {
                    offsetX = x - Left;
                    isMoving = true;
                }
            }

            foreach (object oc in Controls)
            {
                Control c = (Control)oc;
                c.touchDown(o, e);
            }
        }

        int lastX = 0;
        int lastY = 0;
        public void touchscreen_TouchMoveEvent(object o, TouchEventArgs e)
        {
            if (!Visible) return;

            if (isMoving)
            {
                if (lastX != e.x || lastY != e.y)
                {
                    lastX = e.x - offsetX;
                    lastY = e.y;

                    if (lastX < 0) lastX = 0;
                    if (lastY < 0) Top = 0;
                    if (lastX > Parent.screen.Width - Width) lastX = Parent.screen.Width - Width;
                    if (lastY > Parent.screen.Height - Height) lastY = Parent.screen.Height - Height;

                    Parent.screen.DrawRectangle(lastX, lastY, lastX + Width, lastY + Height, BorderColor);
                }
            }

            foreach (object oc in Controls)
            {
                Control c = (Control)oc;
                c.touchMove(o, e);
            }
        }

        public void touchscreen_TouchUpEvent(object o, TouchEventArgs e)
        {
            if (!Visible) return;

            if (isMoving)
            {
                int x = e.x;
                int y = e.y;

                Left = x - offsetX;
                Top = y;

                if (Left < 0) Left = 0;
                if (Top < 0) Top = 0;
                if (Left > Parent.screen.Width - Width) Left = Parent.screen.Width - Width;
                if (Top > Parent.screen.Height - Height) Top = Parent.screen.Height - Height;

                isMoving = false;

                Parent.Redraw();
            }

            foreach (object oc in Controls)
            {
                Control c = (Control)oc;
                c.touchUp(o, e);
            }
        }

#endif
        public void Add(Control control)
        {
            if (control != null)
            {
                Controls.Add(control);
            }
        }

        public object Add(ControlTypes control, int x, int y)
        {
            switch (control)
            {
                case ControlTypes.Label:
                    Label l = new Label(x, y, BackgroundColor, CaptionColor, "", this);
                    Controls.Add(l);
                    return l;
                case ControlTypes.CheckBox:
                    CheckBox c = new CheckBox(x, y, "Check", CaptionColor, BackgroundColor, ColorExtensions.RGB16(0, 255, 0), this);
                    Controls.Add(c);
                    return c;
                case ControlTypes.RadioButton:
                    RadioButton r = new RadioButton(x, y, new string[1] { "Option 1" }, CaptionColor, BackgroundColor, ColorExtensions.RGB16(0, 255, 0), this);
                    Controls.Add(r);
                    return r;
                case ControlTypes.ScrollingGraph:
                    ScrollingGraph s = new ScrollingGraph(x, y, 10, 2, BackgroundColor, ColorExtensions.RGB16(0, 255, 0),1000, this);
                    Controls.Add(s);
                    return s;
                case ControlTypes.Image:
                    Image i = new Image(x, y, 0,0, this);
                    Controls.Add(i);
                    return i;
                case ControlTypes.Button:
                    Button m = new Button(x, y, "Button", this);
                    Controls.Add(m);
                    return m;
                case ControlTypes.ListBox:
                    ListBox li = new ListBox(x, y, 10, 10, BackgroundColor, CaptionColor, null, this);
                    Controls.Add(li);
                    return li;
                case ControlTypes.Frame:
                    Frame f = new Frame(x,y,10,10,this);
                    Controls.Add(f);
                    return f;
            }
            return null;
        }

        public void Draw()
        {
            if (!Visible) return;

            if (!FullScreen)
            {
                Parent.screen.DrawFilledRectangle(Left, Top, Left + Width, Top + Height, BackgroundColor);

                if (Text != null)
                {
                    Parent.screen.DrawFilledRectangle(Left, Top - 2, Left + Width, Top + Parent.screen.CharacterHeight - 1, CaptionColor);
                    Parent.screen.DrawText(Text, Left, Top, false, CaptionColor, ForeColor);
                }

                if (DrawBorder)
                {
                    Parent.screen.DrawRectangle(Left, Top - 2, Left + Width, Top + Height, BorderColor);
                }
            }
            else
            {
                if (Gradient)
                {
                    //switch (GradientMode)
                    //{
                        //case GradientType.Red: screen.SPE.GradientY(GradientStart, 0, 0, GradientEnd, 0, 0, screen.Width, screen.Height, 4); break;
                        //case GradientType.Green: screen.SPE.GradientY(0, GradientStart, 0, 0, GradientEnd, 0, screen.Width, screen.Height, 4); break;
                        //case GradientType.Blue: screen.SPE.GradientY(0, 0, GradientStart, 0, 0, GradientEnd, screen.Width, screen.Height, 4); break;
                    //}
                }
                else
                {
                    Parent.screen.DrawFilledRectangle(0, 0, Parent.screen.Width, Parent.screen.Height, BackgroundColor);
                }

                if (Effect)
                {
                    Random rnd = new Random();
                    for (int i = 0; i < 30; i++)
                    {
                        int tx = -20 + rnd.Next(Parent.screen.Width + 40);
                        int ty = -20 + rnd.Next(Parent.screen.Height + 40);
                        int tr = rnd.Next(40);

                        if (EffectMode == EffectType.Squares) Parent.screen.DrawRectangle(tx, ty, tx + tr, ty + tr, EffectColor);
                    }
                }

                if (Text != null)
                {
                    Parent.screen.DrawFilledRectangle(0, 0, Parent.screen.Width, Parent.screen.CharacterHeight - 1, CaptionColor);
                    Parent.screen.DrawText(Text, 0, 0, false, CaptionColor, ForeColor);
                }

                if (DrawBorder)
                {
                    Parent.screen.DrawRectangle(Left, Top, Left + Width, Top + Height, BorderColor);
                }
            }
            
        }

        public void Redraw()
        {
            if (!Visible) return;

            Draw();

            foreach (object o in Controls)
            {
                Control c = (Control)o;
                c.Redraw();
            }
        }

        public int cX(int x)
        {
            return x * Parent.screen.CharacterWidth;
        }

        public int cY(int y)
        {
            return y * Parent.screen.CharacterHeight;
        }
    }
}
